package org.luchengye.ioauth2.clientresttemplate.controller;

import org.luchengye.ioauth2.clientresttemplate.user.ClientUser;
import org.luchengye.ioauth2.clientresttemplate.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 */
@Controller
public class RegistryController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @ResponseBody
    @PostMapping("/registry")
    public ModelAndView registry(ClientUser user) {
        System.out.println("注册成功");
        String pwd = passwordEncoder.encode(user.getPassword());
        userRepository.save(new ClientUser(user.getUsername(), pwd));
        return new ModelAndView("redirect:/");
    }
}
